﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsprinttasklogServices : Repository<corecmsprinttasklog>, IcorecmsprinttasklogServices
    {
        public bool Insert(string logMessage)
        {
            corecmsprinttasklog entity = new corecmsprinttasklog();
            entity.createTime = DateTime.Now;
            entity.message = logMessage;
            return base.Change<corecmsprinttasklog>().Insert(entity);
        }

        public async Task<bool> InsertAsync(string logMessage)
        {
            try
            {
                corecmsprinttasklog entity = new corecmsprinttasklog();
                entity.createTime = DateTime.Now;
                entity.message = logMessage;
                return await base.Change<corecmsprinttasklog>().InsertAsync(entity);
            }
            catch (Exception ex) { }
            return false;
        }

        
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsprinttasklog> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsprinttasklog>().Update(a => new corecmsprinttasklog()
                {
                    createTime = item.createTime,
                    message = item.message
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsprinttasklog>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsprinttasklog>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsprinttasklog> GetCaChe()
        {
            return base.GetList();
        }
        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprinttasklog> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, Expressionable<corecmsprinttasklog> where)
        {
            List<corecmsprinttasklog> custos = new List<corecmsprinttasklog>();
            //var where = Expressionable.Create<corecmsprinttasklog>();
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .OrderBy(a => a.id, OrderByType.Desc).ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id, OrderByType.Desc).ToList();
            }
            return custos;
        }
        #endregion

    }
}